package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.AddDishDTO;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishPageQueryItemVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorMapper flavorMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 增加菜品
     * */
    @Override
    public void addDish(AddDishDTO addDishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(addDishDTO,dish);
        dishMapper.insert(dish);
        List<DishFlavor> flavors=addDishDTO.getFlavors();
        for(DishFlavor flavor:flavors){
            flavor.setDishId(dish.getId());
            flavorMapper.insert(flavor);
        }
        //删缓存
        String key=RedisConstant.KEY_PREFIX+"categoryId:"+addDishDTO.getCategoryId();
        stringRedisTemplate.delete(key);


        //  flavorMapper.insertBatch(flavors);

    }
    /**
     * 分页查询
     * */

    @Override
    public PageResult<DishPageQueryItemVO> page(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishPageQueryItemVO>page= dishMapper.page(dto);
        return new PageResult<DishPageQueryItemVO>(page.getTotal(),page.getResult());
    }
    /**
     * 删除菜品
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return;
        }
        for(Long id: ids) {
            Dish dish = dishMapper.selectById(id);
            if(Objects.equals(dish.getStatus(), StatusConstant.ENABLE)){
                throw new BaseException(MessageConstant.DISH_ON_SALE);
                // Long categoryId = dish.getCategoryId();
            }
            //判断是否关联了套餐TOOD
            dishMapper.deleteById(id);
            flavorMapper.deleteByDishId(id);
            //删缓存
            String key=RedisConstant.KEY_PREFIX+"categoryId:"+dish.getCategoryId();
            stringRedisTemplate.delete(key);
//




        }


    }
    /**
     * 修改回显
     * */
    @Override
    public DishVO getById(Long id) {
        Dish dish= dishMapper.selectById(id);
        if(dish==null){
            throw new BaseException("菜品不存在");

        }
        List<DishFlavor>dishFlavors=flavorMapper.selectById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;




    }
    /**
     * 修改菜品
     * */

    @Override
    public void update(DishDTO dishDTO) {
        //更新菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //  dishMapper.update(dish);
        dishMapper.update(dish);



        //删除口味
        flavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(f->f.setDishId(dishDTO.getId()));
            flavorMapper.insertBatch(dishDTO.getFlavors());
        }
        //删缓存
        String key=RedisConstant.KEY_PREFIX+"categoryId:"+dish.getCategoryId();
        stringRedisTemplate.delete(key);






        //插入口味


    }
    /**
     * 增加套餐菜品回显
     * */
    @Override
    public List<Dish> addsetmeal(Long categoryId) {
        List<Dish> dish = dishMapper.selectBycategoryId(categoryId);
        return dish;


    }
    /**
     * 小程序端根据分类ID 查询菜品
     * */

    @Override
    public List<DishVO> seleteByCategoryId(Long categoryId) {
        String key= RedisConstant.KEY_PREFIX+"categoryId:"+categoryId;
        //查询redis
        String json = stringRedisTemplate.opsForValue().get(key);
        //redis有直接返回
        if(StringUtils.hasLength(json)){
            //json转list
            List<DishVO> dishVOListlist = JSON.parseArray(json, DishVO.class);
            return dishVOListlist;
        }
        List<DishVO>list=  dishMapper.seleteByCategoryId(categoryId);
        //数据库不空写入redis
        if(!CollectionUtils.isEmpty(list)){
            stringRedisTemplate.opsForValue().set(key,JSON.toJSONString(list));
        }
        return list;
    }
}
